package department

import (
	"ginfwk/adapter/base/controller"
	dto "ginfwk/application/dto/admin"
	"ginfwk/common/mock/admin"
	"ginfwk/common/mock/mhelper"
	"ginfwk/infrastructure/helper/ghttp"
	"github.com/gin-gonic/gin"
)

type Department struct {
	controller.BaseController
}

func (c *Department) Options(g *gin.Context) interface{} {
	c.Init(g)

	departmentList := admin.GetDepartmentList()

	return ghttp.Success(gin.H{
		"list": departmentList,
	})
}

func (c *Department) List(g *gin.Context) interface{} {
	c.Init(g)

	type ListInfo struct {
		PageIndex      int    `json:"pageIndex" form:"pageIndex"`
		PageSize       int    `json:"pageSize" form:"pageSize"`
		DepartmentName string `json:"departmentName" form:"departmentName"`
		DepartmentCode string `json:"departmentCode" form:"departmentCode"`
	}
	var info ListInfo
	if err := g.ShouldBindQuery(&info); err != nil {
		return ghttp.Fail(-1, "信息错误:"+err.Error())
	}

	departmentList := admin.GetDepartmentList()
	pageList := mhelper.Pagination(departmentList, info.PageIndex, info.PageSize)

	return ghttp.Success(gin.H{
		"total": len(departmentList),
		"list":  pageList,
		"info":  info,
	})
}

func (c *Department) Save(g *gin.Context) interface{} {
	c.Init(g)

	type DepartmentInfo struct {
		Id             string `json:"id"`
		Status         int    `json:"status"`
		DepartmentName string `json:"departmentName"`
		Remark         string `json:"remark"`
	}

	var info DepartmentInfo
	if err := g.ShouldBindJSON(&info); err != nil {
		return ghttp.Fail(-1, "信息错误:"+err.Error())
	}

	return ghttp.Success(gin.H{
		"id":   "198147-14133136-463104-104141215-111718",
		"info": info,
	})
}

func (c *Department) Info(g *gin.Context) interface{} {
	type Param struct {
		Id string `json:"id" form:"id"`
	}
	var param Param
	if err := g.ShouldBindQuery(&param); err != nil {
		return ghttp.Fail(-1, "信息错误:"+err.Error())
	}

	var info dto.Department
	itemList := admin.GetDepartmentList()
	for _, itemInfo := range itemList {
		if itemInfo.Id == param.Id {
			info = itemInfo
		}
	}
	return ghttp.Success(info)
}

func (c *Department) Delete(g *gin.Context) interface{} {
	c.Init(g)

	type DeleteInfo struct {
		Ids []string `json:"ids"`
	}

	var info DeleteInfo
	if err := g.ShouldBindJSON(&info); err != nil {
		return ghttp.Fail(-1, "信息错误:"+err.Error())
	}

	return ghttp.Success(info)
}
